package com.crcode.crm.controller.system;

import com.alibaba.fastjson.JSONObject;
import com.crcode.crm.bean.system.NoticeBean;
import com.crcode.crm.bean.user.UserBean;
import com.crcode.crm.service.system.NoticeService;
import com.crcode.crm.utils.DateUtil;
import com.crcode.crm.utils.PageVo;
import com.crcode.crm.utils.ResultPageUtil;
import com.crcode.crm.utils.ResultUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * 公告控制层
 * @author xiangfei
 */
@RestController
@RequestMapping("/system/notice")
public class NoticeController {

    @Resource(name="noticeService")
    private NoticeService noticeService;

    /**
     * 查看公告，获取详情
     * @param request
     * @param session
     * @param id
     * @return
     */
    @RequestMapping(value="/get", method = RequestMethod.GET)
    public ResultUtil getOne(HttpServletRequest request, HttpSession session, Integer id){
        ResultUtil resultUtil = new ResultUtil();
        NoticeBean noticeBean = noticeService.getById(id);
        if(noticeBean != null && noticeBean.getId() != null) {
            resultUtil.setInfo("获取成功");
            resultUtil.setObj(noticeBean);
            resultUtil.setStatus(true);
        } else {
            resultUtil.setInfo("找不到公告");
            resultUtil.setObj(null);
            resultUtil.setStatus(false);
        }
        return resultUtil;
    }

    /**
     * 列出公告，可搜索标题
     * @param request request
     * @param session session
     * @param text 搜索内容
     * @param vo vo
     * @return
     */
    @RequestMapping(value="/list", method = RequestMethod.POST)
    public ResultPageUtil list(HttpServletRequest request, HttpSession session, PageVo vo, String text){
        return this.noticeService.findByNoticeTitleContaining(text, vo);
    }


    /**
     * 删除多个公告,一个公告也可以调用此方法
     * @return
     */
    @RequestMapping(value="/delete", method = RequestMethod.POST)
    public ResultUtil deleteOne(HttpServletRequest request, HttpSession session, String ids){
        ResultUtil resultUtil = new ResultUtil();
        Object obj = session.getAttribute("user");
        if(obj == null) {
            resultUtil.setStatus(false);
            resultUtil.setInfo("未登录，请先登录");
            resultUtil.setObj(null);
            return resultUtil;
        }
        UserBean user = JSONObject.parseObject(obj.toString(), UserBean.class);
        if(user == null){
            resultUtil.setStatus(false);
            resultUtil.setInfo("未登录，请先登录");
            resultUtil.setObj(null);
            return resultUtil;
        }
        List<String> idList= Arrays.asList(ids.split(","));
        List<Integer> idRealList = new ArrayList<>();
        for (String str: idList) {
            idRealList.add(Integer.parseInt(str));
        }
        boolean success = this.noticeService.deleteByIdIn(idRealList);
        if(!success) {
            resultUtil.setInfo("操作失败!");
            resultUtil.setStatus(success);
            resultUtil.setObj(null);
            return resultUtil;
        }
        resultUtil.setInfo("操作成功");
        resultUtil.setStatus(success);
        resultUtil.setObj(null);
        return resultUtil;
    }

    /**
     * 新增或编辑公告
     * @param request
     * @param session
     * @param bean
     * @return
     */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public Map<String,Object> saveOrUpdate(HttpServletRequest request, HttpSession session, NoticeBean bean) {
        Map<String,Object> map = new HashMap<>();
        Object obj = session.getAttribute("user");
        UserBean user = JSONObject.parseObject(obj.toString(), UserBean.class);
        if(user == null){
            map.put("status", false);
            map.put("info", "用户未登录，请先登录");
            return map;
        }
        boolean success = false;
        if(bean != null) {
            if(bean.getId() != null) { // 有id，在修改
                NoticeBean old_bean = this.noticeService.getById(bean.getId());
                if(old_bean != null && old_bean.getId() != null) {
                    old_bean.setNoticeTitle(bean.getNoticeTitle());
                    old_bean.setCreateTime(bean.getCreateTime());
                    old_bean.setNoticeContent(bean.getNoticeContent());
                    old_bean.setUserName(bean.getUserName());
                    old_bean.setUserUuid(bean.getUserUuid());
                    success = this.noticeService.saveResultBoolean(bean);
                }
                map.put("status", success);
                map.put("info", "保存成功");
                return map;
            }
            // 没有id,新增
            bean.setUserName(user.getUserRealName());
            bean.setUserUuid(user.getUuid());
            bean.setCreateTime(DateUtil.DateToStr(new Date(),"yyyy-MM-dd HH:mm:ss"));
            success = this.noticeService.saveResultBoolean(bean);
            map.put("status", success);
            map.put("info", "保存成功");
            return map;
        }

        map.put("status", false);
        map.put("info", "保存/更新失败");
        return map;
    }
}
